package org.mogu.control;

import java.util.HashMap;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.mogu.R;
import org.mogu.Welcome;
import org.mogu.db.DBhelper;
import org.mogu.service.InfoSync;
import org.mogu.staticvalue.Convert;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.text.InputFilter;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class Replay5 extends RelativeLayout {
	TextView title;
//	EditText edittext;
	Button cancelbtn;
//	Button replaybtn;
	Button sendbtn;
	Replay5 replay5;
	String code;
	String replayType;
	String maincode;
//	String uname;
	String father;
//	String pwd;
	Welcome welcome;
	String check;
	
	
	
	LinearLayout linegroup;

	DefaultHttpClient client;
	HttpResponse httpResponse;
	Map<String, String> limt=new HashMap<String, String>();

	public Replay5(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}

	public Replay5(Context context, AttributeSet attrs) {
		super(context, attrs);
		LayoutInflater.from(context).inflate(R.layout.replay5, this, true);
		
		linegroup=(LinearLayout)findViewById(R.id.replay5content);
		
		cancelbtn = (Button) findViewById(R.id.weibo_cancel);
		cancelbtn.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				welcome.goBack();
			}
		});
		sendbtn = (Button) findViewById(R.id.weibo_ok);
		sendbtn.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				new Thread() {
					public void run() {
				try {
					String c="";
					EditText edit;
//					int l;
					int num=0;
					for(int i=0;i<linegroup.getChildCount();i++){
						if(linegroup.getChildAt(i) instanceof EditText){
							num++;
							edit= (EditText)linegroup.getChildAt(i);
							//if(edit.getText().toString()==null||"".equals(edit.getText().toString())||Integer.valueOf(limt.get(String.valueOf(num))).intValue()>=edit.getText().toString().length()){
							c+=edit.getText().toString()+"**";
						}
					}
					
					String url;
					url = Convert.hosturl + "/AddReply?UserName="
							+ Convert.uname + "&UserPwd=" +Convert.upwd+"&Code="+code+"&MainCode="+maincode+"&Type="+replayType+"&Content="+c;
//					url="http://weibo.zxxsbook.com/login?username=11&website=wy";
					
					client = new DefaultHttpClient();
					
					
					HttpGet getMethod = new HttpGet(url+ Convert.testNum);

					httpResponse = client.execute(getMethod);

					if (httpResponse.getStatusLine().getStatusCode() == 200) {
						// 取出回应字串
						String strResult;
						strResult = EntityUtils.toString(httpResponse
								.getEntity());
//						if ("1".equals(strResult)) {
//							Toast.makeText(welcome, "提交失败，稍后再试。", 4000).show();
//						}
						if ("0".equals(strResult)) {
							if("a999-9-s1".equals(father)){
								Convert.upwd=c.split("\\*\\*")[1];
								ContentValues activeuser = new ContentValues();
								String[] selectionArgs={Convert.uname};
								activeuser.put("password", Convert.upwd);
								synchronized (InfoSync.class) {
								SQLiteDatabase writedb = DBhelper.getDBHelper(welcome).getWritableDatabase();
								writedb.update("activeuser", activeuser, "username=?",
										selectionArgs);
								}
							}
							
						}
					}
					
				} catch (Exception e1) {
					e1.printStackTrace();
				}finally{
					
				}
					}}.start();
					welcome.goBack();
					Toast.makeText(welcome, "提交成功", 4000).show();
				
			}
		});
		
	}

	public void setContent(String code,String father, String replayType, String maincode,
			 String content) {
//		weibocontent.setText(content);
		linegroup.removeAllViews();
		this.code = code;
		this.replayType = replayType;
		this.maincode = maincode;
		this.father=father;
		int intId=1;
		TextView txt;
		EditText edit;
		String[] w=content.split("\\*\\*");
		String[] t;
//		LinearLayout line;
		for(int i=0;i<w.length;i++){
			if(!"".equals(w[i])){
				//line=new LinearLayout(linegroup.getContext());
				t=w[i].split("#");
				txt=new TextView(linegroup.getContext());
				txt.setTextColor(0xffffffff);
				txt.setTextSize(30);
				txt.setText(t[0]);
				linegroup.addView(txt);
				if(t.length>1){
					edit=new EditText(linegroup.getContext());
					edit.setId(intId);
					edit.setWidth(200);
//					edit.setLayoutParams(new LayoutParams(c, attrs))
					edit.setFilters(new InputFilter[]{new InputFilter.LengthFilter(Integer.valueOf(t[1]))});
					linegroup.addView(edit);
					//limt.put(String.valueOf(intId), t[1]);
					intId++;
					if(t.length>2){
						txt=new TextView(linegroup.getContext());
						txt.setTextColor(0xffffffff);
						txt.setTextSize(30);
						txt.setText(t[2]);
						linegroup.addView(txt);
					}
					
				}
				//linegroup.addView(line);
			}
		}
		
	}

	

	public void setReplay(Replay5 content, Welcome welcome) {
		replay5 = content;
		this.welcome = welcome;
	}

}
